package toDo.app.controllers;

import java.util.Calendar;

import interfaces.IToDoListActivity;
import toDo.app.R;
import toDo.app.models.TimeAuthority;
import database.DatabaseFacade;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.DatePicker;

public class ToDoListActivitySelectedController implements OnItemSelectedListener, OnDateSetListener {
	private IToDoListActivity activity;
	private Calendar dateAndTime = Calendar.getInstance();

	public ToDoListActivitySelectedController(IToDoListActivity activity) {
		this.activity = activity;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
		// manages what happens when item from spinner menu is selected

		if (parent.getId() == R.id.toDoListSpinner) {
			if (activity.getCategories().get(position).equals(activity.getString(R.string.all))) {
				activity.setList(DatabaseFacade.getList(activity, activity.getUser()));
			} else {
				activity.setList(DatabaseFacade.getListByType(activity, activity.getUser(), activity.getCategories()
						.get(position)));
			}
		} else if (parent.getId() == R.id.toDoListViewSpinner) {
			activity.setCategoryVisibility(false);
			switch(position) {
				case 0:
					activity.setList(DatabaseFacade.getList(activity, activity.getUser()));
					break;
				case 1:
					activity.setList(DatabaseFacade.getListByCompleted(activity, activity.getUser(), false));
					break;
				case 2:
					new DatePickerDialog(activity, this, dateAndTime.get(Calendar.YEAR),
							dateAndTime.get(Calendar.MONTH), dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
					break;
				case 3:
					activity.setCategoryVisibility(true);
					break;
			}
		}
		// refresh list
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		int date = TimeAuthority.componentTimeToTimestamp(year, monthOfYear, dayOfMonth, 0, 0, 0);
		activity.setList(DatabaseFacade.getListByDate(activity, activity.getUser(), date, true));
		// TODO Auto-generated method stub

	}

}
